package cn.lan.generator.service.impl;

import cn.lan.generator.bean.EntityInfo;
import cn.lan.generator.bean.Module;
import cn.lan.generator.bean.ProjInfo;
import cn.lan.generator.service.CreateFile;
import cn.lan.generator.utils.Constant;
import cn.lan.generator.utils.FileUtil;
import cn.lan.generator.utils.StringUtil;

/**
 * 创建页面文件
 *
 * @author lanping
 * @version 1.0
 * @date 2021-03-01
 **/
public class CreateEntityPageImpl implements CreateFile {


    public static final String [] ENTITY_PAGES = {"entityAdd.html","entityEdit.html","entityList.html"};

    /**
     * 创建页面文件
     *
     * @param entityInfo 实体信息
     * @param projInfo   项目信息
     * @throws Exception
     */
    @Override
    public void writeFile(EntityInfo entityInfo, ProjInfo projInfo) throws Exception {
        for(String fileName:ENTITY_PAGES){
            String filePath = Constant.TEMPLATE_PATH+"/thymeleaf/templates/entity/"+fileName;
            String sourceFilePath = FileUtil.getResourceRootDir(projInfo.isDebugModel())+filePath;

            String lowerEntityName = StringUtil.toLowerCaseFirstOne(entityInfo.getEntityName());
            String suffixPath = lowerEntityName+"\\"+ fileName.replace("entity",lowerEntityName);
            String destFilePath = Constant.getFilePath(projInfo, Module.getModule(projInfo).getWebServer(),  Constant.PathType.resource_templates, suffixPath);
            FileUtil.copyFile(projInfo,sourceFilePath,destFilePath);
        }
    }
}
